import { createStore } from "vuex";
import createPersistedState from "vuex-persistedstate";
import {
  Project,
  IncomeStream,
  OutlayStream,
  User,
  ProjectResources,
  SystemSetting
} from "@/interface/entity";

import {
  FormPatternItem,
  FormPattern,
  TablePattern
} from "@/interface/general";

export default createStore({
  state: {
    // show loading when processing request
    isLoading: false,
    username: "" as string,

    nowUser: {} as User,
    nowUserPermissionList: [] as string[],

    userList: [] as User[],

    tableData: [] as User[] | Project[] | IncomeStream[] | OutlayStream[],

    systemSetting: {} as SystemSetting,

    fileNameList: [] as string[],

    projectResources: {} as ProjectResources,

    //
    nowSystem: "",

    projectList: [] as Project[],

    // will be used when adding a stream
    selectedProject: {} as Project,

    //
    selectedRowIndex: 0,

    // when export
    selectRowNameList: [] as {
      label: string;
      value: string;
    }[],

    // income
    // income/outlayAmount streams of a project from the db
    streamList: [] as IncomeStream[] | OutlayStream[],

    // 面包屑导航数组
    breadcrumbArray: [] as Record<string, any>[],

    // 当前正在编辑的表单（有初始值）
    editingForm: {} as IncomeStream | OutlayStream | Project | User,
    // form pattern data
    formPattern: {
      labelWidth: "",
      formItem: [] as FormPatternItem[]
    } as FormPattern,

    // form rules
    formRules: {},

    // table pattern data
    tablePattern: [] as TablePattern[]
  },
  mutations: {
    // logout and remove token
    logout(state) {
      state.username = "";
    }
  },
  actions: {},
  modules: {},
  // vuex-persistedstate
  plugins: [
    createPersistedState({
      storage: window.sessionStorage
    })
  ]
});
